package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.base.BaseException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.PetAdoptionMapper;
import com.ruoyi.system.mapper.PetInfoMapper;
import com.ruoyi.system.domain.PetAdoption;
import com.ruoyi.system.domain.PetInfo;
import com.ruoyi.system.service.IPetAdoptionService;

/**
 * 领养申请Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class PetAdoptionServiceImpl implements IPetAdoptionService 
{
    @Autowired
    private PetAdoptionMapper petAdoptionMapper;

    @Autowired
    private PetInfoMapper petInfoMapper;

    /**
     * 查询领养申请
     * 
     * @param adoptionId 领养申请主键
     * @return 领养申请
     */
    @Override
    public PetAdoption selectPetAdoptionById(Long adoptionId)
    {
        return petAdoptionMapper.selectPetAdoptionById(adoptionId);
    }

    /**
     * 查询领养申请列表
     * 
     * @param petAdoption 领养申请
     * @return 领养申请
     */
    @Override
    public List<PetAdoption> selectPetAdoptionList(PetAdoption petAdoption)
    {
        return petAdoptionMapper.selectPetAdoptionList(petAdoption);
    }

    /**
     * 新增领养申请
     * 
     * @param petAdoption 领养申请
     * @return 结果
     */
    @Override
    public int insertPetAdoption(PetAdoption petAdoption)
    {
        petAdoption.setUserId(SecurityUtils.getUserId());
        petAdoption.setStatus("0");
        petAdoption.setApplyTime(new Date());
        return petAdoptionMapper.insertPetAdoption(petAdoption);
    }

    /**
     * 修改领养申请
     * 
     * @param petAdoption 领养申请
     * @return 结果
     */
    @Override
    public int updatePetAdoption(PetAdoption petAdoption)
    {
        return petAdoptionMapper.updatePetAdoption(petAdoption);
    }

    /**
     * 批量删除领养申请
     * 
     * @param adoptionIds 需要删除的领养申请主键
     * @return 结果
     */
    @Override
    public int deletePetAdoptionByIds(Long[] adoptionIds)
    {
        return petAdoptionMapper.deletePetAdoptionByIds(adoptionIds);
    }

    /**
     * 删除领养申请信息
     * 
     * @param adoptionId 领养申请主键
     * @return 结果
     */
    @Override
    public int deletePetAdoptionById(Long adoptionId)
    {
        return petAdoptionMapper.deletePetAdoptionById(adoptionId);
    }

    /**
     * 审批领养申请
     * 
     * @param petAdoption 领养申请信息
     * @return 结果
     */
    @Override
    @Transactional
    public int approveAdoption(PetAdoption petAdoption)
    {
        petAdoption.setApproveBy(SecurityUtils.getUsername());
        petAdoption.setApproveTime(new Date());

        PetAdoption petAdoptionOld = petAdoptionMapper.selectPetAdoptionById(petAdoption.getAdoptionId());
        if(ObjectUtils.isEmpty(petAdoptionOld)){
            throw new BaseException("申请信息不存在");
        }

        // 如果审批通过，更新宠物状态为已领养
        if ("1".equals(petAdoption.getStatus()))
        {
            PetInfo petInfo = new PetInfo();
            petInfo.setPetId(petAdoptionOld.getPetId());
            petInfo.setStatus("1");
            petInfoMapper.updatePetInfo(petInfo);
        }
        
        return petAdoptionMapper.updatePetAdoption(petAdoption);
    }

    @Override
    public PetAdoption getByPetId(Long petId) {
        return petAdoptionMapper.getByPetId(petId);
    }

    @Override
    public List<PetAdoption> selectPetAdoptionByPetIds(List<Long> petIds) {
        return petAdoptionMapper.selectPetAdoptionByPetIds(petIds);
    }
} 